
package com.user.controller;


import com.common.context.UserContextHolder;
import com.common.domain.pojo.Result;
import com.user.domain.params.SecurityParams;
import com.user.domain.params.UpdateUserParams;
import com.user.domain.pojo.User;
import com.user.domain.vo.SecurityMD5Vo;
import com.user.domain.vo.UserInfo;
import com.user.domain.vo.UserPubInfo;
import com.user.service.IUserService;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小杰大人
 * @since 2025-02-17
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    IUserService iUserService;

    @GetMapping("/detail")
    public Result<User> getUserDetail(){
        User user = iUserService.getDetailById(UserContextHolder.getContext().getUsersId());
        return Result.success(user);
    }

    // 获取指定用户基本信息
    @GetMapping("/info")
    public Result<UserInfo> getUserInfoById(@RequestParam(name = "id", required = false) Long id){
        // 默认可不传 获取当前请求用户信息
        if (id == null){
            id = UserContextHolder.getContext().getUsersId();
        }
        UserInfo userInfo = iUserService.getUserInfoById(id);
        return Result.success(userInfo);
    }

    // 更新用户个人信息
    @PutMapping("/update/detail")
    public Result<String> updateUserDetail(@RequestBody UpdateUserParams userParams){
        iUserService.updateUserDetail(userParams);
        return Result.success();
    }

    // 获取邮箱验证码
    @GetMapping("/validate/sms/email")
    public Result<String> sendSmsCode(@RequestParam("email") String email){
        iUserService.sendSmsEmailCodeByValidate(email);
        return Result.success();
    }

    // 获取手机验证码
    @GetMapping("/validate/sms/phone")
    public Result<String> sendSmsPhone(@RequestParam("phone") String phone){
        iUserService.sendSmsPhoneCodeByValidate(phone);
        return Result.success();
    }

    // 获取用户安全保密信息
    @GetMapping("/security")
    public Result<SecurityMD5Vo> getUserSecurity() {
        SecurityMD5Vo securityMD5Vo = new SecurityMD5Vo();
        User user = iUserService.getById(UserContextHolder.getContext().getUsersId());
        securityMD5Vo.setMdPhone(DigestUtils.md5DigestAsHex(user.getPhone().getBytes()));  // 加密后的手机号
        securityMD5Vo.setMdEmail(DigestUtils.md5DigestAsHex(user.getEmail().getBytes()));  // 加密后的验证码
        return Result.success(securityMD5Vo);
    }

    // 绑定邮箱
    @PostMapping("/security/email")
    public Result<String> bindEmail(@RequestBody SecurityParams securityParams) {
        iUserService.updateEmail(securityParams);
        return Result.success();
    }

    // 绑定手机号
    @PostMapping("/security/phone")
    public Result<String> bindPhone(@RequestBody SecurityParams securityParams) {
        iUserService.updatePhone(securityParams);
        return Result.success();
    }

    // 修改手机号
    @PutMapping("/security/phone")
    public Result<String> updatePhone(@RequestBody SecurityParams securityParams) {
        iUserService.updatePhone(securityParams);
        return Result.success();
    }

    // 修改邮箱
    @PutMapping("/security/email")
    public Result<String> updateEmail(@RequestBody SecurityParams securityParams) {
        iUserService.updateEmail(securityParams);
        return Result.success();
    }

    // 关注用户
    @GetMapping("/follow/{id}/{isFollow}")
    public Result<String> follow(@PathVariable("id") Long followUserId, @PathVariable("isFollow") Boolean isFollow){
        iUserService.follow(followUserId,isFollow);
        return Result.success();
    }
    // 是否关注用户
    @GetMapping("/isFollow/{id}")
    public Result<Boolean> isFollow(@PathVariable("id") Long followUserId){
        Boolean result = iUserService.isFollow(followUserId);
        return Result.success(result);
    }

    // 获取用户关注列表
    @GetMapping("/follow/list")
    public Result<List<UserInfo>> getUserFollowList(@RequestParam(value = "module", defaultValue = "1") Integer module,
                                                         @RequestParam(value = "sorted", defaultValue = "1") Integer sorted,
                                                         @RequestParam(value = "query", defaultValue = "all") String query){
        List<UserInfo> userFollowList = iUserService.getUserFollowList(module,sorted,query);
        return Result.success(userFollowList);
    }

    // 获取用户公开信息
    @GetMapping("/public/info/{id}")
    public Result<UserPubInfo> getUserPublicInfo(@PathVariable("id") Long id){
        UserPubInfo userPubInfo = iUserService.getUserPublicInfo(id);
        return Result.success(userPubInfo);
    }

    // 查询用户信息 根据用户唯一标识
    @GetMapping("/detail/infoByUniqueIdentification")
    public Result<User> querySelectByUniqueIdentification(@RequestParam("UniqueIdentification") Object UniqueIdentification){
        User user = iUserService.querySelectByUniqueIdentification(UniqueIdentification);
        return Result.success(user);
    }

    // 查询用户信息列表 根据用户id集合  key=用户id value=用户信息
    @PostMapping("/detail/list")
    public Result<Map<Long, UserInfo>> querySelectUserListByIds(@RequestBody List<Long> ids){
        return Result.success(iUserService.querySelectUserListByIds(ids));
    }

    // 获取用户粉丝数量
    @GetMapping("/fans/count")
    public Result<Long> getUserFansCount(@RequestParam("userId") Long userId){
        Long count = iUserService.getUserFansCount(userId);
        return Result.success(count);
    }

}